<?php


/**
 * Delegate RequestDispatcher. Provide mechanism to access to request variables. 
 * 
 * @package SpotLight.core.HTTP
 * @author asmhack
 * @version since begin
 * @license GPL
 * @copyright Serhiy Zaporozhets'
 */
class Request {
   
   private static $dispatcher = null;
   
   private function __construct() {
   }
   private function __destruct() {
   }
   
   public static function getAttribute($key) {
      self::getRequestDispatcher ();
      return self::$dispatcher->isExist ( $key ) ? self::$dispatcher->getAttribute ( $key ) : null;
   }
   
   public static function setAttribute($key, $value) {
      self::getRequestDispatcher ();
      self::$dispatcher->setAttribute ( $key, $value );
   
   }
   
   public static function getAttributes() {
      self::getRequestDispatcher ();
      return self::$dispatcher->getAttributes ();
   }
   
   private static function getRequestDispatcher() {
      if (self::$dispatcher != null)
         return;
      $package = Session::getSystemValue ( 'confMap' );
      $disp = $package->getDispatcher ( 'request' );
      
      if (file_exists ( $disp->getClass () . '' ))
         @include_once ($disp->getClass () . '');
      else {
         die ( 'Request dispatcher file not found: <b>' . $disp->getClass () . '</b>' );
      }
      $className = self::extractClassName ( $disp->getClass () );
      if (! class_exists ( $className ))
         die ( 'Request dispatcher class is not found<b>' . $className . '</b> in file <b>' . $disp->getClass () . '</b>' );
      
      $dispReflectionClass = new ReflectionClass ( $className );
      
      self::$dispatcher = $dispReflectionClass->newInstance ();
      
      if (! is_subclass_of ( self::$dispatcher, 'RequestDispatcher' )) {
         self::$dispatcher = null;
         die ( 'RequestDispatcher inheritance error!!!' );
      }
      return self::$dispatcher->dispatch ();
   }
   
   public static function dispatchURI() {
      if (self::$dispatcher != null)
         return self::$dispatcher->dispatch (  );
      else
         return self::getRequestDispatcher ();
   }
   
   public static function isExist($key) {
      self::getRequestDispatcher ();
      return self::$dispatcher->isExist ( $key );
   }
   
   public static function clear() {
      self::getRequestDispatcher ();
      self::$dispatcher->clear ();
   }
   
   private static function extractClassName($path) {
      $slash = split ( '/', $path );
      $slash = $slash [count ( $slash ) - 1];
      $slash = split ( '\.', $slash );
      return $slash [0];
   }
}

?>